$(function () {
    let layer = layui.layer;
    let form = layui.form;
    initArtCateList();

    function initArtCateList() {
        $.ajax({
            method: "GET",
            url: "/my/article/cates",
            success: function (res) {
                if (res.status !== 0) return layer.msg('获取文章分类失败');
                //layer.msg('获取文章分类成功')
                let htmlStr = template('tpl-table', res);
                $('tbody').html(htmlStr);
            }
        })
    }

    let indexAdd = null;
    $('#btnAddCate').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章分类',
            content: $('#dialog-add').html(),
            area: ['500px', '250px']
        });
    })

    $('body').on('submit', '#form-add', function (e) {
        e.preventDefault();
        $.ajax({
            method: "POST",
            url: "/my/article/addcates",
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) return layer.msg('新增文章分类失败');
                layer.msg('新增文章分类成功')
                initArtCateList();
                layer.close(indexAdd);
            }
        })
    })

    let indexEdit = null;
    $('tbody').on('click', '.btn-edit', function () {
        indexEdit = layer.open({
            type: 1,
            title: '修改文章分类',
            content: $('#dialog-edit').html(),
            area: ['500px', '250px']
        });


        let id = $(this).attr('data-id');
        // let id=$(this).data('id');
        $.ajax({
            method: "GET",
            url: "/my/article/cates/" + id,
            success: function (res) {
                if (res.status !== 0) return layer.msg('根据id获取文章分类失败');
                //layer.msg('根据id获取文章分类成功')
                form.val('form_edit', res.data);
            }
        })
    })


    $('body').on('submit', '#form-edit', function (e) {
        e.preventDefault();
        form.val()
        $.ajax({
            method: "POST",
            url: "/my/article/updatecate",
            data: $(this).serialize(),
            success: function (res) {
                if (res.status !== 0) return layer.msg('修改文章分类失败');
                layer.msg('修改文章分类成功')
                initArtCateList();
                layer.close(indexEdit);
            }
        })
    })


    $('tbody').on('click', '.btn-delete', function () {
        let id = $(this).attr('data-id');

        //eg1
        layer.confirm('确定删除?', { icon: 3, title: '提示' }, function (index) {
            //do something
            $.ajax({
                method: "GET",
                url: "/my/article/deletecate/" + id,
                success: function (res) {
                    if (res.status !== 0) return layer.msg('删除文章分类失败');
                    layer.msg('删除文章分类成功');
                    initArtCateList();
                    layer.close(index);
                }
            })
            
        });


        
        
      
    })


})